cli-table
Rust crate for printing tables on command line.
Usage
Add cli-table
in your Cargo.toms
's dependencies
section
[]
= "0.4"
Simple usage
use ;
let table = vec!
.table
.title
.bold;
assert!;
Below is the output of the table we created just now:
+------------+----------------+
Display
trait implementation
To get a Display
trait implementation of TableStruct
, use display()
function on the struct to get an instance
of TableDisplay
which implements Display
trait.
use ;
let table = vec!
.table
.title
.bold;
let table_display = table.display.unwrap;
println!;
Below is the output of the table we created just now:
+------------+----------------+
Derive macro
#[derive(Table)]
can also be used to print a Vec
or slice of struct
s as table.
use ;
let users = vec!;
assert!;
Below is the output of the table we created using derive macro:
+----+------------+-----------+
Field attributes
title
|name
: Used to specify title of a column. Usage:#[table(title = "Title")]
justify
: Used to horizontally justify the contents of a column. Usage:#[table(justify = "Justify::Right")]
align
: Used to vertically align the contents of a column. Usage:#[table(align = "Align::Top")]
color
: Used to specify color of contents of a column. Usage:#[table(color = "Color::Red")]
bold
: Used to specify boldness of contents of a column. Usage:#[table(bold)]
order
: Used to order columns in a table while printing. Usage:#[table(order = <usize>)]
. Here, columns will be sorted based on their order. For e.g., column withorder = 0
will be displayed on the left followed by column withorder = 1
and so on.display_fn
: Used to print types which do not implementDisplay
trait. Usage#[table(display_fn = "<func_name>")]
. Signature of provided function should befn <func_name>(value: &<type>) -> impl Display
.customize_fn
: Used to customize style of a cell. Usage#[table(customize_fn = "<func_name>")]
. Signature of provided function should befn <func_name>(cell: CellStruct, value: &<type>) -> CellStruct
. This attribute can be used when you want to change the formatting/style of a cell based on its contents. Note that this will overwrite all the style settings done by other attributes.skip
: Used to skip a field from table. Usage:#[table(skip)]
For more information on configurations available on derive macro, go to cli-table/examples/struct.rs
.
CSV
This crate also integrates with csv
crate. On enabling "csv"
feature, you can
use TryFrom<&mut Reader> for TableStruct
trait implementation to convert csv::Reader
to TableStruct
.
For more information on handling CSV values, go to cli-table/examples/csv.rs
.
Styling
Style of a table/cell can be modified by calling functions of [Style
] trait. It is implementated by both
[TableStruct
] and [CellStruct
].
For individually formatting each cell of a table, justify
, align
and padding
functions can be used from
CellStruct
.
In addition to this, borders and separators of a table can be customized by calling border
and separator
functions in TableStruct
. For example, to create a borderless table:
use ;
let table = get_table.border; // Attaches an empty border to the table
assert!;
Features
derive
: Enables derive macro for creating tables using structs. Enabled by default.csv
: Enables support for printing tables usingcsv
. Enabled by default.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.